package fr.emse.tatiana.analysis.annotation;



import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.EditorPart;

import fr.emse.tatiana.remotecontrol.RemoteControl;
import fr.emse.tatiana.replayable.ContinuousReplayable;
import fr.emse.tatiana.replayable.IArtefactEditor;
import fr.emse.tatiana.replayable.SynchronisationEvent;
import fr.emse.tatiana.replayable.analysis.AnalysisEvent;
import fr.emse.tatiana.replayable.analysis.IAnalysis;
import fr.emse.tatiana.replayable.analysis.IAnalysisEditor;


public class AnnotationAnalysisEditor extends EditorPart implements IAnalysisEditor {

	public static final String ID = "fr.emse.tatiana.AnalysisEditor";
	
	protected AnnotationAnalysis model;
	protected TableViewer viewer;
	protected Table table;
	
	public AnnotationAnalysisEditor() {
		super();
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO z later use monitor
		if (!model.hasAnalysisFile()) {
			doSaveAs();
		} else {
			model.doSave();
			firePropertyChange(PROP_DIRTY);
		}
	}

	@Override
	public void doSaveAs() {
		InputDialog dialog = new InputDialog(this.getSite().getShell(), 
				"Save analysis as...", 
				"Name of new analysis:", null, null); 
		if (dialog.open() != Window.OK)
			return;
		model.doSave(dialog.getValue());
		setPartName(model.getName());
		firePropertyChange(PROP_DIRTY);
		firePropertyChange(PROP_TITLE);
	}
	
	public void dispose() {
		IEditorReference[] editors = this.getSite().getWorkbenchWindow().getActivePage().getEditorReferences();
		for (int i=0; i<editors.length; i++) {
			IEditorPart ed = editors[i].getEditor(false);
			if (ed instanceof IArtefactEditor) {
				((IArtefactEditor) ed).removeAnalysis(this.model);
			}
		}
		model.removeAnalysisEventListener(this);
		super.dispose();
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		try {
			this.model = (AnnotationAnalysis) ((AnnotationAnalysisInput) input).getModel();
		} catch (Exception e) {
			throw new PartInitException("Not an analysis");
		}
		setInput(input);
		setSite(site);
		setPartName(input.getName());
		
		IEditorReference[] editors = this.getSite().getWorkbenchWindow().getActivePage().getEditorReferences();
		for (int i=0; i<editors.length; i++) {
			IEditorPart ed = editors[i].getEditor(false);
			if (ed instanceof IArtefactEditor) {
				((IArtefactEditor) ed).addAnalysis(this.model);
			}
		}
		
		model.addAnalysisEventListener(this);
		
	}

	@Override
	public boolean isDirty() {
		return !model.isSaved();
	}

	@Override
	public boolean isSaveAsAllowed() {
		return true;
	}

	@Override
	public void createPartControl(Composite parent) {
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns=1;
		gridLayout.marginHeight=0;
		gridLayout.marginWidth=0;
		parent.setLayout(gridLayout);
		
		


		viewer = new TableViewer(parent,SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
		table = viewer.getTable();
		GridData griddata = new GridData(GridData.FILL,GridData.FILL,true,true);
		table.setLayoutData(griddata);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		viewer.setContentProvider(model.getContentProvider());
		viewer.setLabelProvider(model.getLabelProvider());
		viewer.setCellModifier(model.getCellModifier());
		String[] names = model.getColumnNames();
		Rectangle area = table.getParent().getClientArea();
		ScrollBar vBar = table.getVerticalBar();
		int width = area.width - table.computeTrim(0,0,0,0).width - vBar.getSize().x;
		for(int col=0; col<names.length; col++) {
			TableColumn column = new TableColumn(table,SWT.LEFT);
			column.setText(names[col]);
			column.pack();
		}
		for(int col=0; col<names.length; col++) {
			table.getColumns()[col].setWidth(model.getWidth(col));
		}
		
		viewer.setColumnProperties(names);
		viewer.setCellEditors(model.getCellEditors(table));
		viewer.setInput(model);
	}

	@Override
	public void setFocus() {
		// do nothing

	}

	/* (non-Javadoc)
	 * @see fr.emse.tatiana.replayable.analysis.IAnalysisEditor#getAnalysis()
	 */
	public AnnotationAnalysis getAnalysis() {
		return model;
	}

	/* (non-Javadoc)
	 * @see fr.emse.tatiana.replayable.analysis.IAnalysisEditor#analysisEventOccurred(fr.emse.tatiana.replayable.analysis.AnalysisEvent)
	 */
	public void analysisEventOccurred(AnalysisEvent evt) {
		switch(evt.getType()) {
		case AnalysisEvent.ELEMENT_CHANGE:
		case AnalysisEvent.LINK_CHANGE:
		case AnalysisEvent.ELEMENT_ADD:
		case AnalysisEvent.ELEMENT_REMOVE:
			firePropertyChange(PROP_DIRTY);
			break;
		case AnalysisEvent.TITLE_CHANGE:
			firePropertyChange(PROP_TITLE);
			firePropertyChange(PROP_DIRTY);
			break;
		}
		
	}

	public void addAnalysis(IAnalysis a) {
		// do nothing
		
	}

	public void removeAnalysis(IAnalysis a) {
		// do nothing
		
	}

	public void setRemote(RemoteControl remote) {
		// do nothing
		
	}

	public List<Long> getEvents() {
		return new ArrayList<Long>();
	}

	public void end() {
		// do nothing
		
	}

	public void goTo(SynchronisationEvent event) {
		// do nothing
		
	}

	public void mark(long time) {
		// do nothing
		
	}

}
